package main

import (
	"context"
	"gitlab.yctc.tech/zhiting/disk-manager.git/pkg/proto"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"log"
)

// 简单的gRPC测试demo
func main() {
	conn, err := grpc.Dial("192.168.0.97:9090", grpc.WithInsecure())
	//conn, err := grpc.Dial("127.0.0.1:9090", grpc.WithInsecure())
	//conn, err := grpc.Dial("192.168.0.123:9090", grpc.WithInsecure())
	if err != nil {
		log.Fatalln(err)
	}
	defer conn.Close()
	client := proto.NewDiskManagerClient(conn)
	ctx := context.Background()
	log.Println("starting vg request...")
	rvgs, err := client.VolumeGroupList(ctx, &proto.Empty{})
	if err != nil {
		handleError(err)
	} else {
		for _, vg := range rvgs.VGS {
			log.Printf("vg: %v, %v, %v, size %v, free %v", vg.Name, vg.Exported, vg.UUID, vg.Size, vg.FreeSize)
			for _, pv := range vg.PVS {
				log.Printf("pv: %v, size %v, %v", pv.Name, pv.Size, pv.UUID)
			}
			for _, lv := range vg.LVS {
				log.Printf("lv: %v, %v, size: %v, freesize %v, %v", lv.Name, lv.Path,
					lv.Size, lv.FreeSize, lv.UUID)
			}
		}
	}

	log.Println("----------------------------------------------------")
	vgRemoveReq :=proto.VolumeGroupRemoveReq{
		VGName: "npc",
	}
	_, err = client.VolumeGroupRemove(ctx, &vgRemoveReq)
	if err != nil {
		log.Printf("error %v/n", err.Error())
	}else {
		log.Println("vg remove success")
	}
	log.Println("----------------------------------------------------")

	//vgCreateReq := proto.VolumeGroupCreateOrExtendReq{
	//	VGName: "npc",
	//	PVName: "/dev/sda",
	//}
	//vgr, err := client.VolumeGroupCreate(ctx, &vgCreateReq)
	//if err != nil {
	//	handleError(err)
	//}
	//log.Println("created vg: %v", vgr.Data)

	//vgRenameReq := proto.VolumeGroupRenameReq{
	//	OldName: "npc",
	//	NewName: "npccc",
	//}
	//vgr, err := client.VolumeGroupRename(ctx, &vgRenameReq)
	//if err != nil {
	//	handleError(err)
	//}
	//log.Println("rename vg: %v", vgr.Data)

	log.Println("starting vg request...")
	rvgs, err = client.VolumeGroupList(ctx, &proto.Empty{})
	if err != nil {
		handleError(err)
	} else {
		for _, vg := range rvgs.VGS {
			log.Printf("vg: %v, %v, %v, size %v, free %v", vg.Name, vg.Exported, vg.UUID, vg.Size, vg.FreeSize)
			for _, pv := range vg.PVS {
				log.Printf("pv: %v, size %v, %v", pv.Name, pv.Size, pv.UUID)
			}
			for _, lv := range vg.LVS {
				log.Printf("lv: %v, %v, size: %v, freesize %v, %v", lv.Name, lv.Path,
					lv.Size, lv.FreeSize, lv.UUID)
			}
		}
	}

	//log.Println("starting pv request...")
	//rpvs, err := client.PhysicalVolumeList(ctx, &proto.Empty{})
	//if err != nil {
	//	handleError(err)
	//}
	//for _, pv := range rpvs.PVS {
	//	log.Printf("pv: %v, %v, %v, %v", pv.Name, pv.VGName, pv.UUID, pv.Size)
	//}

	//log.Println("starting create lv...")
	//lvcReq := proto.LogicalVolumeCreateReq{
	//	VGName: "yctc",
	//	LVName: "lva",
	//	SizeM:  100,
	//}
	//lvc, err := client.LogicalVolumeCreate(ctx, &lvcReq)
	//if err != nil {
	//	log.Fatalf("create lv error: %v", err)
	//	return
	//}
	//log.Printf("lv created: %v", lvc.Data.Path)

	//lvrReq := proto.LogicalVolumeRenameReq{
	//	VGName:    "yctc",
	//	LVName:    "lva",
	//	NewLVName: "lvc",
	//}
	//lvc, err := client.LogicalVolumeRename(ctx, &lvrReq)
	//if err != nil {
	//	log.Fatalf("rename lv error: %v", err)
	//	return
	//}
	//log.Printf("lv rename: %v", lvc.Data.Name)

	//lveReq := proto.LogicalVolumeExtendReq{
	//	VGName:   "yctc",
	//	LVName:   "lvc",
	//	NewSizeM: 200,
	//}
	//lvc, err := client.LogicalVolumeExtend(ctx, &lveReq)
	//if err != nil {
	//	log.Fatalf("extend lv error: %v", err)
	//	return
	//}
	//log.Printf("lv extend: %v, %v", lvc.Data.Name, lvc.Data.Size)

	//lvrmReq := proto.LogicalVolumeRemoveReq{
	//	VGName: "yctc",
	//	LVName: "lvc",
	//}
	//_, err = client.LogicalVolumeRemove(ctx, &lvrmReq)
	//if err != nil {
	//	log.Fatalf("remove lv error: %v", err)
	//	return
	//}
}

func handleError(err error) {
	if errStatus, ok := status.FromError(err); ok {
		if errStatus.Code() == codes.NotFound {
			return
		}
		log.Fatalf("error %v : %v", errStatus.Code(), errStatus.Message())
	} else {
		log.Fatalln(err)
	}
}
